package com.qianyu.project.service.smj;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.ApplyProgressPayment;
import com.qianyu.project.mapper.provider.smj.smjApplyDao;
import com.qianyu.project.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Service
public class smjApplyServiceImpl implements smjApplyService {
    @Autowired
    private smjApplyDao ad;

    @Override//添加进度款申报
    public String AddApply(String apply_date, String p_id, String ctr_id, String company_id, String payment_name, String contract_amount, String penalty, String withhold, String settle_type, String agree_amount, String agree_upper, String handler_id, String update_user, String settle_comment, String attach_ids) {
        String update_date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String[] ids = update_date.split(" ");
        String ids1 =ids[0];
        String ids2 =ids[1];
        String[] ids3 = ids1.split("-");
        String[] ids4 = ids2.split(":");
        String id ="";
        for (int i = 0; i <ids3.length ; i++) {
             id += ids3[i];
        }
        for (int i = 0; i <ids4.length ; i++) {
            id += ids4[i];
        }
        ad.AddApply(apply_date,p_id, ctr_id, company_id, payment_name, contract_amount, penalty, withhold, settle_type, agree_amount, agree_upper, handler_id, update_user, settle_comment, attach_ids,update_date,id);

        return id;
    }

    @Override
    public void worklist(String apply_id, String work_content, String work_count, String unit, String unit_price, String snumber, String hnumber, String comment) {
        Integer sum_price = Integer.valueOf(unit_price)*Integer.valueOf(work_count);
        ad.worklist( apply_id, work_content, work_count, unit, unit_price,sum_price, snumber, hnumber, comment);
    }

    @FlowBegin("apply_progress_payment")//发起审批
    @Override
    public Integer FlowApply(@FlowEntry("id") ApplyProgressPayment applyProgressPayment) {
        ad.UpdateApproveStatus(1, applyProgressPayment.getId());
        return 1;
    }

    @Override//查询进度款信息
    public IPage<Map<String, Object>> listApply(Map<String,String> params) {
        IPage<Map<String, Object>> page= PageUtil.createPage(params);
        return ad.listApply(params,page);
    }

    @Override//进度款申请的工程量清单
    public List<Map<String, Object>> getWorkList(String id) {
        return ad.getWorkList(id);
    }

    @Override//流程审批查询
    public List<Map<String, Object>> getfromaudit(String id) {
        String fid = ad.getFid(id);
        return ad.getfromaudit(fid);
    }
}
